#pragma once

#include "defines.h"
#include "Cursor.h"
#include "PenteBoardNode.h"
#include "screen.h"

class PenteBoard
{
private:
	//Board Object
	PenteBoardNode* board[BOARD_SIZE * BOARD_SIZE];

	//player turn flag
	int whichPlayer;

public:
	//ctor
	PenteBoard(): whichPlayer(0)
	{
		for(int row = 0; row < BOARD_SIZE; row++ )
		{
			for(int col = 0; col < BOARD_SIZE; col++ )
			{
				int temp = row * BOARD_SIZE + col; // thank you Anthony :)
				board[temp] = new PenteBoardNode( row, col );
				board[temp]->initializeBoardNode();
			}
		}
	}
	//dtor
	~PenteBoard()
	{
		for(int i = 0; i < BOARD_SIZE * BOARD_SIZE; i++ )
		{
			delete board[i];
			board[i] = NULL;
		}
	}
	int getRow( int x ){ return x / BOARD_SIZE; }
	int getCol( int y ){ return y % BOARD_SIZE; }
	int RowCol( int x, int y ){ return x * BOARD_SIZE + y; }

	void switchActivePlayer()
	{
		++whichPlayer;
		whichPlayer %= PLAYERS;
	}

	int getActivePlayer(){ return whichPlayer; }
	PenteBoardNode *getNode( int which ) { return board[which < BOARD_SIZE * BOARD_SIZE ? which : 0]; }
	PenteBoardNode *getNodeAt( int x, int y ) { return board[( x < BOARD_SIZE && y < BOARD_SIZE ? RowCol( x, y ) : 0 )]; }
	
	void initializeBoard();

	void updateBoard();

	static int getWidth() { return BOARD_SIZE * NODE_COL; }
	static int getHeight() { return BOARD_SIZE * NODE_ROW; }

	void printBoard( Screen *disp )
	{
		for( int i = 0; i < BOARD_SIZE * BOARD_SIZE; i++ )
		{
			board[i]->printBoardNode( disp );
		}
	}
	
};